﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace baseClass.controls
{
    public partial class emGeneral : common.control.baseUserControl
    {
        public emGeneral()
        {
            InitializeComponent();
            SetMaxLength();
            codeEd.isToUpperCase = true;
            eTagEd.BackColor = common.settings.sysColorDisableBG; 
            imageLbl.Text = "(" + emImage.Width.ToString()+"x"+emImage.Height.ToString()+")";
            emImage.noImageFilePath = application.Settings.sysDefaultImageFile;

        }
        private void SetMaxLength()
        {
            data.baseDS.employeeDataTable tbl = new data.baseDS.employeeDataTable();
            codeEd.MaxLength = tbl.emCodeColumn.MaxLength;
            fullNameEd.MaxLength = tbl.fullNameColumn.MaxLength;
            addressEd1.MaxLength = tbl.address1Column.MaxLength;
            addressEd1.MaxLength = tbl.address2Column.MaxLength;
            phoneEd.MaxLength = tbl.phone1Column.MaxLength;
            mobileEd.MaxLength = tbl.mobileColumn.MaxLength;
            emailEd.MaxLength = tbl.email1Column.MaxLength;
        }

        public virtual void Init() 
        {
            sexCb.LoadData();
        }
        public virtual void SetData(data.baseDS.employeeRow row)
        {
            codeEd.Text = row.emCode;
            fullNameEd.Text = row.fullName;
            if (!row.IsemImageNull()) emImage.myImage = common.images.Byte2Image(row.emImage);
            else emImage.myImage = null;

            sexCb.myValue = (application.myTypes.sex)row.sex;
            addressEd1.Text = row.address1;
            addressEd2.Text = (row.Isaddress2Null()?"":row.address2);
            phoneEd.Text = (row.Isphone1Null() ? "" : row.phone1);
            mobileEd.Text = (row.IsmobileNull() ? "" : row.mobile);
            emailEd.Text = (row.Isemail1Null() ? "" : row.email1);
        }
        public virtual void GetData(data.baseDS.employeeRow row)
        {
            row.emCode = codeEd.Text;
            if (emImage.myImage == null) row.SetemImageNull();
            else row.emImage = common.images.Image2Bytes(emImage.myImage);
            row.fullName = fullNameEd.Text;
            row.sex = (byte)sexCb.myValue;
            row.address1 = addressEd1.Text;
            row.address1  = addressEd1.Text.Trim();
            if (addressEd2.Text.Trim() == "") row.Setaddress2Null(); else row.address2 = addressEd2.Text.Trim();
            if (phoneEd.Text.Trim() == "") row.Setphone1Null(); else row.phone1 = phoneEd.Text.Trim();
            if (mobileEd.Text.Trim() == "") row.SetmobileNull(); else row.mobile =mobileEd.Text.Trim();
            if (emailEd.Text.Trim() == "") row.Setemail1Null(); else row.email1 = emailEd.Text.Trim();
        }
        public virtual bool CheckData()
        {
            bool retVal = true;
            ClearNotifyError();
            if (codeEd.Text.Trim() == "")
            {
                NotifyError(codeLbl);
                retVal = false;
            }
            if (fullNameEd.Text.Trim() == "") 
            {
                NotifyError(fullNameLbl);
                retVal = false;
            }
            if (sexCb.myValue == application.myTypes.sex.None) 
            {
                NotifyError(sexLbl);
                retVal = false;
            }
            if (addressEd1.Text.Trim() == "") 
            {
                NotifyError(addressLbl1);
                retVal = false;
            }
            return retVal;
        }
        public virtual void LockEdit(bool state)
        {
            codeEd.ReadOnly = state;
            fullNameEd.ReadOnly = state;
            addressEd1.ReadOnly = state;
            addressEd2.ReadOnly = state;
            phoneEd.ReadOnly = state;
            mobileEd.ReadOnly = state;
            emailEd.ReadOnly = state;

            eTagEd.Enabled = !state;
            eTagScanBtn.Enabled = !state;

            sexCb.Enabled = !state;
            imLoadBtn.Enabled = !state;
        }
        public virtual void SetFocus()
        {
            this.Focus();
            codeEd.Focus();
        }

        private void imageBtn_Click(object sender, EventArgs e)
        {
            try
            {
                bool retVal = true;
                Image img = common.images.GetImage(emImage.myImage, out retVal);
                if (retVal) emImage.myImage = img;
            }
            catch (Exception er)
            {
                ErrorHandler(sender, er);
            }
        }
    }
}
